package cn.stronglink.asset.module.sys.user.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.model.Role;
import cn.stronglink.asset.model.User;
import cn.stronglink.asset.module.sys.department.service.DepartmentService;
import cn.stronglink.asset.module.sys.department.vo.DepartmentTreeNodeVo;
import cn.stronglink.asset.module.sys.role.service.RoleService;
import cn.stronglink.asset.module.sys.user.service.UserService;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.exception.BusinessException;

@Controller
@RequestMapping(value = "sys/user")
public class UserController extends AbstractController {
	
	@Autowired
	private DepartmentService departmentService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private UserService userService;

	@RequestMapping(value = "qryDepts")
	public ResponseEntity<ModelMap> qryDepts(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {
		List<DepartmentTreeNodeVo> vo = departmentService.getTree();
		return setSuccessModelMap(modelMap, vo);
	}
	
	@RequestMapping(value = "qryRoles")
	public ResponseEntity<ModelMap> qryRoles(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String, Object> params) {
		List<Role> list = roleService.qryRoles(params);
		return setSuccessModelMap(modelMap, list);
	}
	
	@RequestMapping(value = "qryUsers")
	public ResponseEntity<ModelMap> qryUsers(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String, Object> params) {
		Page<User> page = (Page<User>) super.getPage(params);
		User user = new User();
		if(params.get("account") != null){
			user.setAccount(params.get("account").toString());
		}
		if(params.get("name") != null){
			user.setName(params.get("name").toString());
		}
		if(params.get("phone") != null){
			user.setPhone(params.get("phone").toString());
		}
		if(params.get("email") != null){
			user.setEmail(params.get("email").toString());
		}
		if(params.get("status") != null && params.get("status").toString() != ""){
			user.setStatus(Integer.valueOf(params.get("status").toString()));
		}
		if(params.get("deptId") != null && params.get("deptId").toString() != ""){
			user.setDeptId(Long.valueOf(params.get("deptId").toString()));
		}
		Page<User> data = userService.getUserByParams(page,user);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 查询所有用户
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "qryAllUser")
	public ResponseEntity<ModelMap> qryAllUser(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {
		List<User> data = userService.qryAllUser();
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 查询某个组织下的所有用户
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "qryUserByDept")
	public ResponseEntity<ModelMap> qryUserByDept(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String, Object> params) {
		List<User> data = userService.qryUserByDept(params);
		return setSuccessModelMap(modelMap, data);
	}
	
	@RequestMapping(value = "qryUserById")
	public ResponseEntity<ModelMap> qryUserById(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Long id) {
		User user = userService.qryUserById(id);
		List<Long> list = userService.getUserRoleIds(id);
		user.setRole(list);
		return setSuccessModelMap(modelMap, user);
	}
	
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody User user) {
		userService.insertUser(user);
		return setSuccessModelMap(modelMap, null);
	}
	
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody User user) {
		userService.updateUser(user);
		return setSuccessModelMap(modelMap, null);
	}
	
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		userService.removeUser(id);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 添加角色接口
	 * @param role
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "createRole")
	public ResponseEntity<ModelMap> createRole(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Role role) {
		int i = roleService.getRoleByName(role.getName());
		if(i > 0){
			throw new BusinessException("角色已经存在！");
		}
		roleService.insertRole(role);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 删除角色
	 * @param id
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "delRole")
	public ResponseEntity<ModelMap> delRole(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		Integer counts=roleService.getRoleUsers(id);
		if(counts>0){
			throw new BusinessException("角色包含用户，不能删除");
		}
		roleService.delRole(id);
		return setSuccessModelMap(modelMap, null);
	}
	
	
	/**
	 * 更新角色信息
	 * @param role
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "updateRole")
	public ResponseEntity<ModelMap> delRole(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Role role) {
		roleService.updateRoleById(role);
		return setSuccessModelMap(modelMap, null);
	}
	
	
	/**
	 * 修改密码
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "updatePass")
	public ResponseEntity<ModelMap> updatePass(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String, Object> params) {
		userService.updatePass(params);
		return setSuccessModelMap(modelMap, null);
	}
	
	
	
}
